package central.stu.dlbasedvisualperceptioncontrol;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    private Socket connection;
    private PrintWriter printer;
    private Button but_SET_LINEAR_VELOCITY;
    private Button but_SET_ANGULAR_VELOCITY;
    private Button but_MOVE_FRONT_METER;
    private Button but_MOVE_BACK_METER;
    private Button but_TURN_ANGULAR;
    private Button but_AUTO_TURN_RIGHT;
    private Button but_AUTO_TURN_LEFT;
    private Button but_CLOSE_CONNECTION;
    private Button but_MOVE_FRONT_VELOCITY;
    private Button but_MOVE_BACK_VELOCITY;
    private Button but_STOP_LINEAR;
    private Button but_STOP_ANGULAR;
    private Button but_FULL_STOP;
    private Button but_SET_HEAD_YAW;
    private Button but_SET_WORLD_PITCH;
    private Button but_connect;
    private EditText editText_SET_LINEAR_VELOCITY;
    private EditText editText_SET_ANGULAR_VELOCITY;
    private EditText editText_MOVE_FRONT_METER;
    private EditText editText_MOVE_BACK_METER;
    private EditText editText_TURN_ANGULAR;
    private EditText editText_IP;
    private EditText editText_Port;
    private EditText editText_MOVE_FRONT_VELOCITY;
    private EditText editText_MOVE_BACK_VELOCITY;
    private EditText editText_SET_HEAD_YAW;
    private EditText editText_SET_WORLD_PITCH;
    private void initContents(){
        this.but_connect = (Button)findViewById(R.id.button_connect);
        this.but_SET_LINEAR_VELOCITY = (Button) findViewById(R.id.button_SET_LINEAR_VELOCITY);
        this.but_SET_ANGULAR_VELOCITY = (Button) findViewById(R.id.button_SET_ANGULAR_VELOCITY);
        this.but_MOVE_FRONT_METER = (Button) findViewById(R.id.button_MOVE_FRONT_METER);
        this.but_MOVE_BACK_METER = (Button) findViewById(R.id.button_MOVE_BACK_METER);
        this.but_TURN_ANGULAR = (Button) findViewById(R.id.button_TURN_ANGULAR);
        this.but_AUTO_TURN_RIGHT = (Button) findViewById(R.id.button_AUTO_TURN_RIGHt);
        this.but_AUTO_TURN_LEFT = (Button) findViewById(R.id.button_AUTO_TURN_LEFT);
        this.but_CLOSE_CONNECTION = (Button) findViewById(R.id.button_CLOSE_CONNECTION);
        this.but_MOVE_FRONT_VELOCITY = (Button) findViewById(R.id.button_MOVE_FRONT_VELOCITY);
        this.but_MOVE_BACK_VELOCITY = (Button) findViewById(R.id.button_MOVE_BACK_VELOCITY);
        this.but_STOP_LINEAR = (Button) findViewById(R.id.button_STOP_LINEAR);
        this.but_STOP_ANGULAR = (Button) findViewById(R.id.button_STOP_ANGULAR);
        this.but_FULL_STOP = (Button) findViewById(R.id.button_FULL_STOP);
        this.but_SET_HEAD_YAW = (Button) findViewById(R.id.button_SET_HEAD_YAW);
        this.but_SET_WORLD_PITCH = (Button) findViewById(R.id.button_SET_WORLD_PITCH);
        this.editText_SET_LINEAR_VELOCITY = (EditText) findViewById(R.id.editText_SET_LINEAR_VELOCITY);
        this.editText_SET_ANGULAR_VELOCITY = (EditText) findViewById(R.id.editText_SET_ANGULAR_VELOCITY);
        this.editText_MOVE_FRONT_METER = (EditText) findViewById(R.id.editText_MOVE_FRONT_METER);
        this.editText_MOVE_BACK_METER = (EditText) findViewById(R.id.editText_MOVE_BACK_METER);
        this.editText_TURN_ANGULAR = (EditText) findViewById(R.id.editText_TURN_ANGULAR);
        this.editText_IP = (EditText) findViewById(R.id.editText_IP);
        this.editText_Port = (EditText) findViewById(R.id.editText_port);
        this.editText_MOVE_FRONT_VELOCITY = (EditText) findViewById(R.id.editText_MOVE_FRONT_VELOCITY);
        this.editText_MOVE_BACK_VELOCITY = (EditText) findViewById(R.id.editText_MOVE_BACK_VELOCITY);
        this.editText_SET_HEAD_YAW = (EditText) findViewById(R.id.editText_SET_HEAD_YAW);
        this.editText_SET_WORLD_PITCH = (EditText) findViewById(R.id.editText_SET_WORLD_PITCH);
    }
    private void setListeners(){
        this.but_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        try {
                            connection = new Socket(editText_IP.getText().toString(),Integer.parseInt(editText_Port.getText().toString()));
                            printer = new PrintWriter(connection.getOutputStream());
                        } catch (IOException e) {
                            e.printStackTrace();
                            runOnUiThread(()->{Toast.makeText(MainActivity.this,e.toString(), Toast.LENGTH_LONG).show();});
                        }
                    }
                }.start();
            }
        });
        this.but_SET_LINEAR_VELOCITY.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.SET_LINEAR_VELOCITY);
                        printer.print(' ');
                        printer.print(Float.parseFloat(editText_SET_LINEAR_VELOCITY.getText().toString()));
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_SET_ANGULAR_VELOCITY.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.SET_ANGULAR_VELOCITY);
                        printer.print(' ');
                        printer.print(Float.parseFloat(editText_SET_ANGULAR_VELOCITY.getText().toString()));
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_MOVE_FRONT_METER.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.MOVE_FRONT_METER);
                        printer.print(' ');
                        printer.print(Float.parseFloat(editText_MOVE_FRONT_METER.getText().toString()));
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_MOVE_BACK_METER.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.MOVE_BACK_METER);
                        printer.print(' ');
                        printer.print(Float.parseFloat(editText_MOVE_BACK_METER.getText().toString()));
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_TURN_ANGULAR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.TURN_ANGULAR);
                        printer.print(' ');
                        printer.print(Float.parseFloat(editText_TURN_ANGULAR.getText().toString()));
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_AUTO_TURN_RIGHT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.AUTO_TURN_RIGHT);
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_AUTO_TURN_LEFT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.AUTO_TURN_LEFT);
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_CLOSE_CONNECTION.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.CLOSE_CONNECTION);
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_MOVE_FRONT_VELOCITY.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.MOVE_FRONT_VELOCITY);
                        printer.print(' ');
                        printer.print(Float.parseFloat(editText_MOVE_FRONT_VELOCITY.getText().toString()));
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_MOVE_BACK_VELOCITY.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.MOVE_BACK_VELOCITY);
                        printer.print(' ');
                        printer.print(Float.parseFloat(editText_MOVE_BACK_VELOCITY.getText().toString()));
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_STOP_LINEAR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.STOP_LINEAR);
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_STOP_ANGULAR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.STOP_ANGULAR);
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_FULL_STOP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.FULL_STOP);
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_SET_HEAD_YAW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.SET_HEAD_YAW);
                        printer.print(' ');
                        printer.print(Float.parseFloat(editText_SET_HEAD_YAW.getText().toString()));
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
        this.but_SET_WORLD_PITCH.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run(){
                        printer.print(Headers.SET_WORLD_PITCH);
                        printer.print(' ');
                        printer.print(Float.parseFloat(editText_SET_WORLD_PITCH.getText().toString()));
                        printer.print(' ');
                        printer.flush();
                    }
                }.start();

            }
        });
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        enableFullScreen();
        initContents();
        setListeners();

    }
    private void enableFullScreen() {
        ActionBar actionBar = getSupportActionBar();
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (actionBar != null) actionBar.hide();
    }
}